package cn.sofwin.web.blog.dao;

import cn.sofwin.core.dao.BaseDao;
import cn.sofwin.core.utils.JdbcUtils;
import cn.sofwin.web.blog.entity.Article;
import cn.sofwin.web.blog.entity.User;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 文章数据访问层
 * @author ZX
 * @version 1.0
 */
public class ArticleDao extends BaseDao<Article> {

    /** 自定义一个BeanHandler，用于封装单个文章对象 */
    private ResultSetHandler<Article> beanHandler = rs -> {
        rs.next();

        Article article = new Article();
        article.setId(rs.getInt("id"));
        article.setTitle(rs.getString("title"));
        article.setSummary(rs.getString("summary"));
        article.setContent(rs.getString("content"));

        Timestamp addTime = rs.getTimestamp("add_time");
        article.setAddTime( new Date(addTime.getTime()));

        article.setReadTotal(rs.getInt("read_total"));
        article.setCommentTotal(rs.getInt("comment_total"));
        article.setStatus(rs.getInt("status"));

        // 封装用户信息
        User user = new User();
        user.setId(rs.getInt("user_id"));
        user.setNickname(rs.getString("nickname"));
        article.setUser(user);

        return article;
    };

    /** 自定义一个结果集处理器，用于封装用户信息 */
    private ResultSetHandler<List<Article>> listHandler = rs -> {
        List<Article> articles = new ArrayList<>();
        Article article = null;
        while (rs.next()){
            article = new Article();
            article.setId(rs.getInt("id"));
            article.setTitle(rs.getString("title"));
            article.setSummary(rs.getString("summary"));
            article.setContent(rs.getString("content"));

            Timestamp addTime = rs.getTimestamp("add_time");
            article.setAddTime( new Date(addTime.getTime()));

            article.setReadTotal(rs.getInt("read_total"));
            article.setCommentTotal(rs.getInt("comment_total"));

            // 封装用户信息
            User user = new User();
            user.setId(rs.getInt("user_id"));
            user.setNickname(rs.getString("nickname"));
            article.setUser(user);

            // 添加到用户列表
            articles.add(article);
        }

        return articles;
    };

    @Override
    public int insert(Article article) {
        String sql = "insert into article(user_id, title, summary, content, add_time) values (?, ?, ?, ?, ?)";
        return JdbcUtils.insert(
                sql,
                article.getUser().getId(),
                article.getTitle(),
                article.getSummary(),
                article.getContent(),
                article.getAddTime());
    }

    @Override
    public int deleteById(Integer id) {
        return JdbcUtils.update("delete from article where id = ?", id);
    }
    /**伪删除（数据库中没有删除，只改了状态，只在前台不显示）*/
    public void remove(Integer id){
        String sql="update article set status = -1 where id=?";
        JdbcUtils.update(sql,id);
    }

    @Override
    public int update(Article article) {
        String sql = "update article set summary = ?, content = ? where id = ?";
        return JdbcUtils.update(sql, article.getSummary(), article.getContent(), article.getId());
    }

    @Override
    public Article get(Integer id) {
        String sql = "SELECT a.*, u.nickname FROM article a LEFT JOIN user u ON a.user_id = u.id where a.id = ?";
        return JdbcUtils.queryForObject(sql, beanHandler, id);
    }

    @Override
    public List<Article> all() {
        String sql = "SELECT a.*, u.nickname FROM article a LEFT JOIN user u ON a.user_id = u.id where a.status != -1 order by a.id desc";
        return JdbcUtils.queryForList(sql, listHandler);
    }

    public  void addReadTotal(String id){
        String sql="update article set read_total = read_total + 1 where id=?";
        JdbcUtils.update(sql,id);
    }

}